﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QtWidgets"
#include <math.h>
#define PI 3.1415926



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}



MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);
    QPen pen;
    painter.translate(this->width()/2,this->height()/2);

    double sx = ui->sx->text().toDouble();
    double sy = ui->sy->text().toDouble();
    double ex = ui->ex->text().toDouble();
    double ey = ui->ey->text().toDouble();
    double cx = ui->cx->text().toDouble();
    double cy = ui->cy->text().toDouble();

    ArcInfo info = DrawArc(QPointF(sx, sy), QPointF(ex, ey), QPointF(cx, cy), ui->checkBox->checkState() == Qt::CheckState::Checked);
    QPointF mid = ArcTransForm(QPointF(sx, sy), QPointF(ex, ey), QPointF(cx, cy), ui->checkBox->checkState() == Qt::CheckState::Checked);
    ui->midPoint->setText(QString("%1,%2").arg(mid.x()).arg(mid.y()));

    painter.drawArc(info.rectangle, info.startAngle*16, info.spanAngle*16);
}


static inline void ToAngle(double& num)
{
    num = num / PI * 180;
}


ArcInfo MainWindow::DrawArc(QPointF start, QPointF end, QPointF center, bool isAcw)
{
    //计算画弧所需的参数有3个：起始角度、夹角、外切矩形
    double startAngle = 0, spanAngle = 0;
    QRectF rectangle;

    //定义起始向量和终止向量
    QPointF startVector = start - center;
    QPointF endVector = end - center;

    //构建两个复数,一个是起点的复数，一个是终点的复数，两者相除即得到旋转子的复数
    //乘以一个模为1的复数时，不会导致缩放，只会产生旋转，这样的复数就称为旋转子（rotor）
    //逆时针：*旋转子(cos(θ)+sin(θ)i)   顺时针：*旋转子的共轨复数(cos(θ)-sin(θ)i)
    //默认逆时针角度为正，顺时针为负
    //若span角为0，说明弧是一个整圆，spanAngle需修正为360°
    ComplexNum c1(startVector.x(), startVector.y());
    ComplexNum c2(endVector.x(), endVector.y());
    ComplexNum rotor = c2 / c1;

    if (isAcw) {
        spanAngle = qAtan2(rotor.B(), rotor.A());
        if (spanAngle < 0) {
            spanAngle += 2*PI;
        }
    } else {
        spanAngle = -qAtan2(-rotor.B(), rotor.A());
        if (spanAngle > 0) {
            spanAngle -= 2*PI;
        }
    }

    if(fabs(spanAngle) < 0.0001) spanAngle = 2 * PI;

    //计算起始角
    c1.setComplexNumValue(1, 0);
    c2.setComplexNumValue(startVector.x(),startVector.y());
    rotor = c2 / c1;
    startAngle = qAtan2(rotor.B(), rotor.A());

    //弧度转角度
    ToAngle(spanAngle);
    ToAngle(startAngle);
    ui->span->setText(QString::number(spanAngle));

    double r = sqrtf(pow(startVector.x(), 2) + pow(startVector.y(), 2));
    //按照鼠标拉扯手势对矩形左上角偏移量进行调整
    QPointF upperLeftPointOfRect = QPointF(center.x()-r, -(center.y()+r));
    rectangle.setRect(upperLeftPointOfRect.x(), upperLeftPointOfRect.y(), 2 * r, 2 * r);

    return {startAngle, spanAngle, rectangle};
}



static double GetSpanAngle(QPointF start, QPointF end, QPointF center, bool isAcw)
{
    double spanAngle = 0;

    //定义起始向量和终止向量
    QPointF startVector = start - center;
    QPointF endVector = end - center;

    ComplexNum c1(startVector.x(), startVector.y());
    ComplexNum c2(endVector.x(), endVector.y());
    ComplexNum rotor = c2 / c1;

    if (isAcw) {
        spanAngle = qAtan2(rotor.B(), rotor.A());
        if (spanAngle < 0) {
            spanAngle += 2*PI;
        }
    } else {
        spanAngle = -qAtan2(-rotor.B(), rotor.A());
        if (spanAngle > 0) {
            spanAngle -= 2*PI;
        }
    }

    if(fabs(spanAngle) < 0.0001) spanAngle = 2 * PI;
    return spanAngle;
}


QPointF MainWindow::ArcTransForm(QPointF start, QPointF end, QPointF center, bool isAcw)
{
    QPointF startVector = start - center;
    double spanAngle = GetSpanAngle(start, end, center, isAcw);

    ComplexNum c1(startVector.x(), startVector.y());
    ComplexNum rotor;
    spanAngle /= 2;
    if (isAcw) {
        rotor.setComplexNumValue(cos(spanAngle), sin(spanAngle));
    } else {
        rotor.setComplexNumValue(cos(spanAngle), -sin(spanAngle));
    }

    ComplexNum mid = c1 * rotor;
    return QPointF(mid.A(), mid.B());
}



ComplexNum::ComplexNum():a(0),b(0)
{

}

ComplexNum::ComplexNum(double a,double b){
    this->a=a;
    this->b=b;
}

//复数的四则运算
ComplexNum ComplexNum::operator +(const ComplexNum& num){
    return ComplexNum(this->a+num.a,this->b+num.b);
}

ComplexNum ComplexNum::operator -(const ComplexNum& num){
    return ComplexNum(this->a-num.a,this->b-num.b);
}

//复数的相乘(a+bi)(c+di)=(ac-bd)+(bc+ad)i两个复数的积仍然是一个复数
ComplexNum ComplexNum::operator *(const ComplexNum& num){
    return ComplexNum(this->a*num.a-this->b*num.b,this->b*num.a+this->a*num.b);
}

//复数的除法
ComplexNum ComplexNum::operator /(const ComplexNum& num){
    if (!num.a && !num.b){
        qDebug()<<"除数不能位0";
        return ComplexNum(a, b);
    }else{
        return ComplexNum((a*num.a + b*num.b) / (num.a*num.a + num.b*num.b),
                          (b*num.a - a*num.b) / (num.a*num.a + num.b*num.b));
    }
}

//其他函数,设置和取模
void ComplexNum::setComplexNumValue(double a,double b){
    this->a=a;
    this->b=b;
}

double ComplexNum::getComplexNumMold(){
    return sqrt(a*a+b*b);
}

double ComplexNum::A(){
    return this->a;
}
double ComplexNum::B(){
    return this->b;
}

void MainWindow::on_pushButton_clicked()
{
    this->update();
}
